﻿using AutoMapper;
using B.S.Data.MES.API.Applications.BaseData.Command.ProductCommand;
using B.S.Domain.CangKu;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.ProductCommandHandler
{
    public class AddGoodCommandHandler : IRequestHandler<AddGoodCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Finished_ProductTest> _goodRepository;
        private readonly IMapper mapper;

        public AddGoodCommandHandler(IBaseRepository<Finished_ProductTest> goodRepository,IMapper mapper)
        {
            _goodRepository = goodRepository;
            this.mapper = mapper;
        }
        public async Task<APIRequst<int>> Handle(AddGoodCommand request, CancellationToken cancellationToken)
        {
            var find= mapper.Map<Finished_ProductTest>(request);
            find.RKState = "未入库";
            Random random = new Random();
            var su = random.Next(0, 2);
            if (su == 0)
            {
                find.ProductState = 1;
            }
            if (su == 1)
            {
                find.ProductState = 2;
            }
            if (su == 2)
            {
                find.ProductState = 3;
            }
            await _goodRepository.Add(find);
            return await Task.FromResult(new APIRequst<int>
            {
                Code=RequstCode.成功,
                Msg=RequstCode.成功.ToString()
            });

        }
    }
}
